﻿using UnityEngine;
using System.Collections;

public class AmmoChart : MonoBehaviour {
    public int m_iThisPrice;

    public int m_iNumOfBuyingObj;
    int m_iBeforeBuyingNum;

    public AmmoBox m_ammoBox;
    public bool m_bExample;
	// Use this for initialization
	void Start () {
        m_iThisPrice = 0;
        m_iNumOfBuyingObj = 0;
	}
	
	// Update is called once per frame
	void Update () {
        if (!m_bExample)
            transform.FindChild("CountBox").FindChild("Label").GetComponent<UILabel>().text = string.Format("{0}", m_iNumOfBuyingObj);
	}

    void LateUpdate()
    {
        if (m_iBeforeBuyingNum != m_iNumOfBuyingObj)
        {
            m_iThisPrice = (m_iNumOfBuyingObj - m_ammoBox.m_iMagazine) * 5;
            transform.FindChild("Price").GetComponent<UILabel>().text = string.Format("$ {0}", m_iThisPrice);
            transform.parent.GetComponent<AmmoBills>().CalculateTotalPrice();
        }

        m_iBeforeBuyingNum = m_iNumOfBuyingObj;
    }

    public void BuyAmmo()
    {
        m_ammoBox.GetComponent<AmmoBox>().m_iMagazine = m_iNumOfBuyingObj;
    }

    public void Init()
    {
        if (m_bExample)
        {
            transform.GetComponent<UISprite>().spriteName = "AmmoBox1";
            transform.FindChild("AmmoBoxName").GetComponent<UILabel>().text = "AmmoDump Name";
            transform.FindChild("Price").GetComponent<UILabel>().text = "Price";
            if (transform.FindChild("AmmoBar") != null)
            {
                Destroy(transform.FindChild("AlreadyHavMag").gameObject);
                Destroy(transform.FindChild("AmmoBar").gameObject);
                Destroy(transform.FindChild("AmmoBarPointer").gameObject);
                Destroy(transform.FindChild("MaxBox").gameObject);
                Destroy(transform.FindChild("CountBox").gameObject);
            }
        }
        else
        {
            transform.FindChild("AmmoBoxName").GetComponent<UILabel>().text = m_ammoBox.m_strBoxName;

            transform.FindChild("CountBox").FindChild("Label").GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
            transform.FindChild("AmmoBarPointer").GetComponent<AmmoBarPointer>().m_iObjNum = m_ammoBox.m_iMaxMag;

            transform.FindChild("AlreadyHavMag").GetComponent<UISprite>().fillAmount = (float)m_ammoBox.m_iMagazine / (float)m_ammoBox.m_iMaxMag;
        }

        //for (int i = 0; i < transform.childCount; ++i)
        //{
        //    if (transform.GetChild(i).GetComponent<UILabel>())
        //        transform.GetChild(i).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
        //    else if (transform.GetChild(i).GetComponent<UISprite>())
        //        transform.GetChild(i).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
        //}
    }
}
